﻿using System;
using System.Linq;
using Exchange.UK;
using System.Collections.Generic;

namespace BetFairProjectUK
{
   public class UKExchangeMain
   {
      public static void MainUK()
      {
      }
      private static APIRequestHeader header = new APIRequestHeader();
      internal static void MainUK(BetFairGlobal.EventType[] ourEvents, string session)
      {
         header.sessionToken = session;
         GetAllMarkets(ourEvents);
         foreach (var x in ourEvents)
         {
            Console.Write("");
         }
      }

      private static void GetAllMarkets(BetFairGlobal.EventType[] ourEvents)
      {
         GetAllMarketsReq req = new GetAllMarketsReq();
         req.eventTypeIds = ourEvents.ToList().Select(item => (int?)item.id).ToArray();
         req.header = header;
         req.fromDate = DateTime.Today.Date;
         req.toDate = DateTime.Today.Add(TimeSpan.FromDays(1)).Date;
         GetAllMarketsResp respo = (new BFExchangeService()).getAllMarkets(req);

         MarketResponse mktResponse = new MarketResponse(respo.marketData);
      }
   }

   public class MarketResponse
   {
      List<MarketInfo> MarketList = new List<MarketInfo>();
      public MarketResponse(string toParse)
      {
         List<String> markets = toParse.Split(':').ToList();
         foreach(var marketString in markets)
         {
            if (String.IsNullOrEmpty(marketString))
            {
               continue;
            }
            var market = marketString.Split('~');
            MarketInfo temp = new MarketInfo(market);
            MarketList.Add(temp);
         }
      }
   }

   public class MarketInfo
   {
      public MarketInfo(string[] market)
      {
         marketIdField = Int32.Parse(market[(int) ValueIndexes.MarketId]);
         nameField = market[(int)ValueIndexes.MarketName];
         marketTypeField = market[(int)ValueIndexes.MarketType];
         marketStatusField = market[(int)ValueIndexes.MarketStatus];
         marketTimeField = DateTime.Parse("January 1 1970 00:00:00 GMT").Add(TimeSpan.FromMilliseconds(long.Parse(market[(int)ValueIndexes.EventDate])));
         menuPathField = market[(int)ValueIndexes.MenuPath];
         eventHierarchyField = market[(int)ValueIndexes.EventHierarchy];
         countryISO3Field = market[(int)ValueIndexes.Iso3];
         numberOfWinnersField = Int32.Parse(market[(int)ValueIndexes.NumOfWinners]);
         bspMarketField = market[(int)ValueIndexes.BSPMarket].Equals("Y");
           
      }

      private enum ValueIndexes
      {
         MarketId,
         MarketName,
         MarketType,
         MarketStatus,
         EventDate,
         MenuPath,
         EventHierarchy,
         BetDelay,
         ExchangeId,
         Iso3,
         LastRef,
         NumOfRunners,
         NumOfWinners,
         TotalMatched,
         BSPMarket, 
         TurningInPlay
      }

      public string countryISO3Field;

      public bool discountAllowedField;

      public int eventTypeIdField;

      public long lastRefreshField;

      public float marketBaseRateField;

      public string marketDescriptionField;

      public bool marketDescriptionHasDateField;

      public System.DateTime marketDisplayTimeField;

      public int marketIdField;

      public string marketStatusField;

      public System.DateTime marketSuspendTimeField;

      public System.DateTime marketTimeField;

      public string marketTypeField;

      public MarketTypeVariantEnum marketTypeVariantField;

      public string menuPathField;

      public string eventHierarchyField;

      public string nameField;

      public int numberOfWinnersField;

      public int parentEventIdField;

      public Runner[] runnersField;

      public string unitField;

      public System.Nullable<double> maxUnitValueField;

      public System.Nullable<double> minUnitValueField;

      public System.Nullable<double> intervalField;

      public bool runnersMayBeAddedField;

      public string timezoneField;

      public int licenceIdField;

      public CouponLink[] couponLinksField;

      public bool bspMarketField;
   }
}
